<?php

declare(strict_types=1);

namespace App;


use Swoole\Coroutine;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;

class Redis
{
    private $pool;


    private static $instance;


    private function __clone()
    {
        // TODO: Implement __clone() method.
    }


    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    private function __construct()
    {
        $this->pool = new RedisPool((new RedisConfig)
            ->withHost(Conf::get('redis.host'))
            ->withPort(Conf::get('redis.port'))
            ->withAuth(Conf::get('redis.auth'))
            ->withDbIndex(Conf::get('redis.index'))
            ->withTimeout(1)
            , Conf::get('redis.max'));
    }

    /**
     * @return RedisPool
     */
    public function getPool()
    {
        return $this->pool;
    }


    /**
     * @return \Redis
     */
    public static function getRedis()
    {
        $redis = self::getInstance()->getPool()->get();
        return $redis;
    }

    public static function putRedis(\Redis $redis)
    {
        self::getInstance()->getPool()->put($redis);
    }

}